home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / egs.lha / EGS / EGS_Devels / Examples / Other / lines.c < prev    next >
C/C++ Source or Header  |  1993-02-17  |  9KB  |  367 lines

  1. /*
  2. **     Author: Markus van Kempen
  3. **
  4. **       Date: 12 Nov 1992
  5. **
  6. **     Compiler : SAS 6.0
  7. **
  8. **
  9. **     This is a line drawing demo for the Amiga or EGS.
  10. **
  11. **     For running under EGS please compile:
  12. **
  13. **     sc link def EGS lines.c
  14. **
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <intuition/intuition.h>
  23. #include <graphics/text.h>
  24. #include <proto/exec.h>
  25. #include <proto/graphics.h>
  26. #include <proto/intuition.h>
  27.  
  28. USHORT wakeup;  /* Wake me up for event */
  29. USHORT class;   /* Intui event class */
  30. USHORT code;    /* Intui event code */
  31.  
  32. UBYTE *oldName;
  33. int oldPri;
  34.  
  35. #ifndef EGS
  36.  
  37. struct Window *w;
  38. struct RastPort *rp,*cdrp;
  39. struct ViewPort *vp;
  40. struct IntuiMessage *message;
  41.  
  42. #define MIX_CLOSEWINDOW CLOSEWINDOW
  43. #define MIX_NEWSIZE     NEWSIZE
  44.  
  45.  
  46.  
  47. #else
  48.  
  49. /*
  50. **   EGS includes & variables
  51. **
  52. **
  53. */
  54.  
  55. #include <egs/egsintui.h>
  56. #include <egs/egsgfx.h>
  57.  
  58. #include <egs/proto/egsintui.h>
  59. #include <egs/proto/egsgfx.h>
  60.  
  61. struct EG_RastPort *rp,*cdrp;
  62. struct EI_Window *w;
  63. struct EI_EIntuiMsg *message;
  64.  
  65. struct Library *EGSGfxBase;
  66. struct Library *EGSIntuiBase;
  67.  
  68.  
  69. #define MIX_CLOSEWINDOW EI_iCLOSEWINDOW
  70. #define MIX_NEWSIZE     EI_iNEWSIZE
  71.  
  72.  
  73. #endif
  74.  
  75. /* Proto */
  76.  
  77. void crash(char *string);
  78. void exit(int i);
  79.  
  80.  
  81. /************************ Window Defines ***********************************/
  82. #ifndef EGS
  83.  
  84. struct NewWindow nw = {
  85.                 100,100,                /* Starting corner */
  86.                 300,100,                /* Width, height */
  87.                 2,1,                    /* detail, block pens */
  88.         MIX_CLOSEWINDOW | MIX_NEWSIZE,          /* IDCMP flags */
  89.         WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO | WINDOWSIZING,
  90.                                         /* Window flags */
  91.                 NULL,                   /* Pointer to first gadget */
  92.                 NULL,                   /* Pointer to checkmark */
  93.                 "Nervous Lines",        /* title */
  94.                 NULL,                   /* screen pointer */
  95.                 NULL,                   /* bitmap pointer */
  96.                 50,50,640,400,          /* window not sized */
  97.                 WBENCHSCREEN            /* type of screen */
  98.                 };
  99.  
  100. #else
  101. /*
  102. **  EGS-EI_Window structure
  103. **
  104. */
  105.  
  106. struct EI_NewWindow nw = {
  107.                 100,100,                /* Starting corner */
  108.                 300,100,                /* Width, height */
  109.                 50,50,640,400,          /* window not sized */
  110.                 NULL,                   /* screen pointer */
  111.                                         /* sysGadget for the Window */
  112.  
  113.                 EI_WINDOWCLOSE | EI_WINDOWSIZE |
  114.                 EI_WINDOWDRAG  | EI_WINDOWBACK |
  115.                 EI_WINDOWFRONT ,
  116.                 NULL,                   /* Pointer to first gadget */
  117.                 "Nervous Lines",        /* title */
  118.                                         /* Flags  */
  119.                 EI_WINDOWACTIVE       |
  120.                 EI_SIZEBBOTTOM         |
  121.                 EI_GIMMEZEROZERO,
  122.                                         /* EIDCMP-FLAGS */
  123.  
  124.                 MIX_CLOSEWINDOW | MIX_NEWSIZE,
  125.  
  126.                 NULL,                   /* MsgPort *    */
  127.                { 3 ,1 ,0 ,2 ,1 ,3 ,1 }, /* WinColors     */
  128.                 NULL,                   /* MenuPtr       */
  129.                 NULL                    /* IntuiGfxPtr   */
  130. };
  131. /*
  132. */
  133.  
  134. #endif
  135.  
  136. int x[2],y[2],xd[2],yd[2],co,ox[2][16],oy[2][16],xx[128],xlim,ylim;
  137.  
  138.  
  139. /**
  140. *
  141. * main program - Since no command line data is used, and no file I/O
  142. * is performed, we do not need _main from the library. We eliminate
  143. * it by calling our main function '_main'.
  144. *
  145. **/
  146. void main()
  147. {
  148.         register short i;
  149.         register int k;
  150.         int      run=1;
  151.         register long co;
  152.         register long j;
  153.         register long rot=0,blau=0,gruen=0;
  154.  
  155. /************************ Set-Up routines **********************************/
  156. /*      Let the auto init code open our libraries for us.                  */
  157.  
  158.  
  159.         oldPri = SetTaskPri(FindTask(0), -5);
  160.         oldName = FindTask(0)->tc_Node.ln_Name;
  161.         FindTask(0)->tc_Node.ln_Name = "Lines";
  162.  
  163. #ifndef EGS
  164.  
  165.         w = OpenWindow(&nw);
  166.         rp = w->RPort;                  /* Get the raster port pointer */
  167.         vp = &w->WScreen->ViewPort;     /* Get the view port pointer */
  168.         SetAPen(rp,3);                  /* Set foreground pen to white */
  169.         SetDrMd(rp,JAM1);               /* Draw with foreground pen */
  170.  
  171.         xlim = w->Width;
  172.         ylim = w->Height;
  173.  
  174.  
  175. #else
  176.  
  177. /*
  178. **  EGS - Open EGS Libraries and call some
  179. **        EGS functions
  180. **
  181. */
  182.         EGSGfxBase = OpenLibrary("egsgfx.library",0);
  183.  
  184.           if (EGSGfxBase == NULL )
  185.                              crash("Can't open egsgfx.library !");
  186.  
  187.         EGSIntuiBase  = OpenLibrary("egsintui.library",0);
  188.  
  189.           if (EGSIntuiBase == NULL )
  190.                              crash("Can't open egsintui.library !");
  191.  
  192.  
  193.         w = EI_OpenWindow(&nw);
  194.         rp = w->RPort;
  195.         EG_SetAPen(rp,0);
  196.         EG_SetDrMd(rp,0);
  197.  
  198.         xlim = w->Width;
  199.         ylim = w->Height;
  200.  
  201. #endif
  202.  
  203.         for(i=0;i<2;i++)
  204.         {
  205.                 x[i] = rand() % xlim + 1;
  206.                 y[i] = rand() % ylim + 1;
  207.                 xd[i] = rand() % 10 + 1;
  208.                 yd[i] = rand() % 10 + 1;
  209.         }
  210.         co = 1;
  211.         j = 0;
  212.  
  213.         do {
  214.  
  215. #ifndef EGS
  216.                 SetAPen(rp,co);
  217.                 Move(rp,x[0],y[0]);
  218.                 Draw(rp,x[1],y[1]);
  219.                 if((rand() & 127) < 2)
  220.                 {
  221.                         ++co;
  222.                         if(co > 3)      co = 1;
  223.                         SetAPen(rp,co);
  224.                 }
  225. #else
  226.  
  227. /*
  228. ** EGS The equal EGS functions
  229. **
  230. **
  231. */
  232.                 EG_SetAPen(rp,co);
  233.                 EG_Move(rp,x[0],y[0]);
  234.                 EG_Draw(rp,x[1],y[1]);
  235.  
  236.  
  237.                 if (blau < 64)
  238.                          blau++;
  239.                 else
  240.                 {
  241.                      blau=0;
  242.  
  243.                      if (gruen < 64)
  244.                                 gruen++;
  245.                      else
  246.                        {
  247.                           gruen=0;
  248.  
  249.                           if (rot < 64)
  250.                                  rot++;
  251.                           else
  252.                           {
  253.                               rot=0;
  254.                           }
  255.  
  256.                        }
  257.                 }
  258.  
  259.  
  260.                 co = ((rot*4)<<24) +
  261.                      ((gruen*4)<<16) +
  262.                      ((blau*4)<<8);
  263.  
  264.                 EG_SetAPen(rp,co);
  265.  
  266. #endif
  267.                 for(i=0;i<2;i++)
  268.                 {
  269.                         ox[i][(j+10) & 15] = x[i];
  270.                         oy[i][(j+10) & 15] = y[i];
  271.                         x[i] += xd[i];
  272.                         y[i] += yd[i];
  273.                         if(x[i] < 2)
  274.                         {
  275.                                 x[i] = 2;
  276.                                 xd[i] = -xd[i];
  277.                         }
  278.                         else if(x[i] > xlim)
  279.                         {
  280.                                 x[i] = xlim;
  281.                                 xd[i] = -xd[i];
  282.                         }
  283.                         if(y[i] < 2)
  284.                         {
  285.                                 y[i] = 2;
  286.                                 yd[i] = -yd[i];
  287.                         }
  288.                         else if(y[i] > ylim)
  289.                         {
  290.                                 y[i] = ylim;
  291.                                 yd[i] = -yd[i];
  292.                         }
  293.                         if(((rand() >> 5) & 127) < 2)
  294.                         {
  295.                                 if(xd[i] < 1)   k = 1;
  296.                                 xd[i] = (rand() >> 5) & 7;
  297.                                 if(k == 1)      xd[i] = -xd[i];
  298.                                 k = 0;
  299.                         }
  300.                         if(((rand() >> 5) & 255) < 50)
  301.                         {
  302.                                 if(yd[i] < 1)   k = 1;
  303.                                 yd[i] = (rand() >> 5) & 7;
  304.                                 if(k == 1)      yd[i] = -yd[i];
  305.                                 k = 0;
  306.                         }
  307.                 }
  308.                 ++j;
  309.  
  310.                 message = (struct EI_EIntuiMsg *)GetMsg(w->UserPort);
  311.                 if(message != NULL)
  312.                 {
  313.                         class = message->Class;
  314.                         code = message->Code;
  315.                         ReplyMsg((struct Message *)message);
  316.                         switch(class)
  317.                         {
  318.                         case MIX_CLOSEWINDOW:
  319.                                 run = 0;
  320.                                 break;
  321.  
  322.                         case MIX_NEWSIZE:
  323.                                 xlim = w->Width;
  324.                                 ylim = w->Height;
  325.                                 break;
  326.                         }
  327.  
  328.                 }
  329.         } while(run);
  330.  
  331. #ifndef EGS
  332.         CloseWindow(w);
  333.         SetTaskPri(FindTask(0), oldPri);
  334.         FindTask(0)->tc_Node.ln_Name = oldName;
  335. #else
  336.         EI_CloseWindow(w);
  337.         crash(NULL);
  338. #endif
  339.  
  340.  
  341. }
  342.  
  343. #ifdef EGS
  344.  
  345. /*
  346. **  CloseLibs
  347. */
  348.  
  349. void crash(char *string)
  350. {
  351.     if (string != NULL)
  352.         printf("%s\n",string);
  353.  
  354.     if (EGSGfxBase != NULL)
  355.         CloseLibrary(EGSGfxBase);
  356.  
  357.     if (EGSIntuiBase != NULL)
  358.         CloseLibrary(EGSIntuiBase);
  359.  
  360.     SetTaskPri(FindTask(0), oldPri);
  361.     FindTask(0)->tc_Node.ln_Name = oldName;
  362.  
  363.     exit(0);
  364. }
  365.  
  366. #endif
  367.